from django import forms, http
from django.shortcuts import redirect, render,HttpResponse
from .models import todolist

import datetime

#创建代办事项
def create_todolist(request):
    if request.method == "GET":
        return render(request,"show_todolist.html")
    
    todo = request.POST.get("todo")
    date = datetime.datetime.now().strftime('%Y-%m-%d')
    todolist.objects.create(date=date,todo=todo)
    return redirect("/show/")

#展示代办事项
def show_todolist(request):
    #默认请求方式为get
    tododata = []
    list = todolist.objects.all()
    for item in list:
        dict = {}
        dict['date'] = item.date.strftime('%Y-%m-%d')
        dict['todo'] = item.todo
        tododata.append(dict)
    return render(request,"show_todolist.html",{"tododata":tododata})

#代办事项已做
def delete_todolist(request):
    todo = request.GET.get("todo")
    todolist.objects.filter(todo=todo).delete()
    return redirect("/show/")